package com.example.sys.component;

import com.example.base.content.NumberContent;
import com.example.sys.entity.SystemBackendInterface;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 统一对Controller做处理 获取所有相关后端接口并插入到数据库中
 */
@Slf4j
@Component
public class ControllerBeanPostProcessor implements BeanPostProcessor, ApplicationRunner {

    private static final List<SystemBackendInterface> BACKEND_INTERFACE_LIST = Lists.newArrayList();

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //获取所有注解 判断是否有 @RequestMapping
        RequestMapping annotation = bean.getClass().getAnnotation(RequestMapping.class);
        if (Objects.isNull(annotation)){
            return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
        }
        String[] paths = annotation.value();
        if (paths.length == 0){
            return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
        }
        //默认只拿第一个
        String path = paths[0];
        //获取所有方法
        Method[] methods = bean.getClass().getMethods();
        for (Method method : methods){
            RequestMapping restPath = method.getAnnotation(RequestMapping.class);
            if (Objects.isNull(restPath)){
                continue;
            }
            String[] values = restPath.value();
            //获取描述
            ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
            String desc = null;
            if (Objects.nonNull(apiOperation)){
                desc = apiOperation.value();
            }
            for (String value : values){
                if (StringUtils.isBlank(value)){
                    continue;
                }
                SystemBackendInterface backendInterface = new SystemBackendInterface();
                backendInterface.setControlFlag(NumberContent.ONE);
                backendInterface.setInterfacePath(path + value);
                backendInterface.setInterfaceDesc(desc);
                backendInterface.setMenuId(Long.valueOf(NumberContent.ONE));
                BACKEND_INTERFACE_LIST.add(backendInterface);
            }
        }
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("后端接口总计: {}", BACKEND_INTERFACE_LIST.size());
    }

    public static List<SystemBackendInterface> getBackendInterfaceList(){
        return Collections.unmodifiableList(BACKEND_INTERFACE_LIST);
    }
}
